﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Collections;

namespace eKanBan.Model
{
    [Serializable]
    class Member : IEnumerable<MemberTask>
    {                        
        private List<MemberTask> history;

        public IList<MemberTask> History { get { return history.AsReadOnly(); } }

        public Member()
        {
            history = new List<MemberTask>();
        }

        #region Properties
        public string Name { get; set; }
        public bool Sex { get; set; }
        public string Comment { get; set; }
        public bool IsDoing { get; set; }
        public Task Doing 
        {
            get
            {
                if (IsDoing && history.Count > 0 && 
                    history[history.Count - 1].FinishTime == DateTime.MinValue)
                {
                    return history[history.Count - 1].Task;
                } 
                else
                    return null;
            }            
        }
        #endregion

        #region currentTask properties
        public DateTime CurTaskStartTime
        {
            get
            {
                if (Doing != null)
                {
                    return history[history.Count - 1].StartTime;
                }
                else
                {
                    throw new Exception("This member is doing nothing");
                }
            }

            set
            {
            	if (Doing != null)
                {
                    if (value >= history[history.Count - 1].FinishTime)
                    {
                        throw new Exception("Cannot set startTime >= finishTime");
                    }
                    history[history.Count - 1].StartTime = value;
                }
                else
                {
                    throw new Exception("This member is doing nothing");
                }
            }
        }

        public DateTime CurTaskFinishTime
        {
            get
            {
                if (Doing != null)
                {
                    return history[history.Count - 1].FinishTime;
                }
                else
                {
                    throw new Exception("This member is doing nothing");
                }
            }

            set
            {
                if (Doing != null)
                {
                    if (value <= history[history.Count - 1].StartTime)
                    {
                        throw new Exception("Cannot set finishTime <= startTime");
                    }
                    history[history.Count - 1].FinishTime = value;
                }
                else
                {
                    throw new Exception("This member is doing nothing");
                }
            }
        }
        #endregion
        public void Take(Task newTask)
        {
            IsDoing = true;
            if (Doing != newTask)
            {
                if (Doing != null)
                {
                    Doing.FinishTime = DateTime.Now;
                    Doing.RemoveMember(this);
                }
                MemberTask tmp = new MemberTask(newTask);
                history.Add(tmp);
                newTask.AddNewMember(this);
            }          
        }

        public void OutOfTask(Task task)
        {
            task.RemoveMember(this);
            IsDoing = false;
        }

        public override string ToString()
        {
            string str = String.Format("Name: {0}, Sex: {1}, Comment: {2}\n", Name, Sex, Comment);
            str += "History:";
            foreach (MemberTask item in history)
            {
                //str += "\n\tTask: " + item.Task.Description;                
                //str += "\n\tStart: " + item.StartTime;
                //str += "\n\tFinish: " + item.FinishTime;
                str += String.Format("\n{0}\n", item);
            }
            return str;
        }

        public IEnumerator<MemberTask> GetEnumerator()
        {
            return history.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return history.GetEnumerator();
        }
    }
}
